package myQueue;

import pojo.Cat;
import pojo.Dog;
import pojo.Pet;

import java.util.LinkedList;

/**
 * 功能描述: 猫狗队列
 *        1. 猫狗队列，要求：
 *        1.1 狗和猫可以入队列，可以出队列，可以判断队列是否为空，可以判断队列是否为空。
 *        1.2 狗和猫的入队列顺序可以保持。

 * 2024/05/23
 * <p>
 * ZhangYi
 */
public class DogAndCatQueue {

    /**
     * 狗队列
     */
    private LinkedList< PetEnterQueue> dogQueue;
    /**
     * cat队列
     */
    private LinkedList< PetEnterQueue> catQueue;
    private int time;

    public DogAndCatQueue() {
        this.dogQueue =  new LinkedList<> ();
        this.catQueue = new LinkedList<> ();
    }

    public void add(PetEnterQueue pet){
        if (pet.getPet() instanceof Dog){
            dogQueue.add(pet);
        }else if (pet.getPet() instanceof Cat){
            catQueue.add(pet    );
        }
    }

    public Pet pollAll(){
        if (!isDogQueueEmpty() && !isCatQueueEmpty()){
            PetEnterQueue dog = dogQueue.peek();
            PetEnterQueue cat = catQueue.peek();
            if (dog.getTime()<cat.getTime()){
                return (Pet) dogQueue.poll().getPet();
            }else {
                return (Pet) catQueue.poll().getPet();
            }
        }
        if (!dogQueue.isEmpty()){
            return (Pet) dogQueue.poll().getPet();
        }
        if (!isCatQueueEmpty()){
            return (Pet) catQueue.poll().getPet();
        }
        return null;
    }

    public Dog pollDog(){
        if (dogQueue.isEmpty()){
            throw new RuntimeException("dog queue is empty");
        }

        return (Dog) dogQueue.poll().getPet();
    }

    public Cat pollCat(){
        if (catQueue.isEmpty()){
            throw new RuntimeException("cat queue is empty");
        }

        return (Cat) catQueue.poll().getPet();
    }

    public boolean isEmpty(){
        return dogQueue.isEmpty() && catQueue.isEmpty();
    }

    public boolean isDogQueueEmpty(){
        return dogQueue.isEmpty();
    }

    public boolean isCatQueueEmpty(){
        return catQueue.isEmpty();
    }

    public static void main(String[] args) {
        DogAndCatQueue dogAndCatQueue = new DogAndCatQueue();
        dogAndCatQueue.add(new PetEnterQueue(new Dog("dog1"),
                                             getTime()));
        dogAndCatQueue.add(new PetEnterQueue(new Cat("cat1"),getTime()));
        dogAndCatQueue.add(new PetEnterQueue(new Dog("dog2"),
                                             getTime()));

        dogAndCatQueue.add(new PetEnterQueue(new Cat("cat2"),getTime()));

        System.out.println("数据压入完成");
        while (!dogAndCatQueue.isEmpty()){

        }
    }

    private static long getTime() {
        return System.currentTimeMillis();
    }
}
